<?PHP
	/************************************************************************
	*       This file, in addition to all other parts of X7 Chat are        *
	*                   Copyright 2003-2007 by Tim Chamness.                *
	* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
	* Version:          X7 Chat 3.0.0-A2                                    *
	* Release Date:     October 20, 2007                                    *
	* Author:           Tim Chamness                                        *
	* Copyright:        2003-2007 by Tim Chamness                           *
	* Website:          http://www.x7chat.com/                              *
	* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
	* X7 Chat 3 is free software; you can redistribute it and/or modify     *
	* it under the terms of the GNU General Public License as published by  *
	* the Free Software Foundation; either version 3 of the License, or     *
	* (at your option) any later version.                                   *
	*                                                                       *
	* X7 Chat 3 is distributed in the hope that it will be useful, but      *
	* WITHOUT ANY WARRANTY; without even the implied warranty of            *
	* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU     *
	* General Public License for more details.                              *
	*                                                                       *
	* You should have received a copy of the GNU General Public License     *
	* along with this program.  If not, see <http://www.gnu.org/licenses/>. *
	*                                                                       *
	* Additional Terms, compatible with Section 7 of the GNU GPL v3,        *
	* apply to X7 Chat 3 and all X7 Chat 3 files, including this one.       *
	* You should have received a copy of these Additional Terms in the      *
	* file docs/COPYING.ADDITIONAL_TERMS.  If you did not receive a copy    *
	* please visit <http://www.x7chat.com/COPYING.ADDITIONAL_TERMS/>.       *
	************************************************************************/

	$uid = x7in::read('uid','i');
	$USER = new lib_user($uid);
	if(!$USER->is_valid()){
		include('./sources/admin/management.php');
		return;
	}
	
	$section = x7in::read('section');
	if(empty($section) || !in_array($section,array('usersettings','userprofile','userpermissions')))
		$section = 'usersettings';
	
	$GLOBALS['OUTPUT']->master_section = 'management';
	$GLOBALS['OUTPUT']->minor_section = $GLOBALS['LANG']->lang_admin->$section;
	$menu = $menus[$GLOBALS['OUTPUT']->master_section];
	
	$menu[$GLOBALS['LANG']->lang_admin->edituser] = 'header';
	$menu[$GLOBALS['LANG']->lang_admin->usersettings] = './index.php?act=admin&page=edituser&uid='.$uid;
	$menu[$GLOBALS['LANG']->lang_admin->userprofile] = './index.php?act=admin&page=edituser&section=userprofile&uid='.$uid;
	$menu[$GLOBALS['LANG']->lang_admin->userpermissions] = './index.php?act=admin&page=edituser&section=userpermissions&uid='.$uid;
	
	$GLOBALS['OUTPUT']->cpmenu = $menu;
	$GLOBALS['OUTPUT']->uid = $uid;
	
	switch($section){
		case 'usersettings':
			
			// Obtain a list of groups
			$groups = array();
			$riterator = $GLOBALS['DATABASE']->get('groups');
			while($gdata = $riterator->get_next()){
				$groups[$gdata['name']] = $gdata['gid'];
			}
			
			$form = array(	'accountsettings'=>array($GLOBALS['LANG']->lang_admin->accountsettings,new form()),
							'generalsettings'=>array($GLOBALS['LANG']->lang_admin->generalsettings,new form()),
							'messagestyles'=>array($GLOBALS['LANG']->lang_admin->messagestyles,new form()),
					);
			
			$form['accountsettings'][1]->add_field('select','group',$GLOBALS['LANG']->lang_admin->group,'',$groups);
			
			$form['generalsettings'][1]->add_field('checkbox','show_styles',$GLOBALS['LANG']->lang_admin->show_styles);
			$form['generalsettings'][1]->add_field('checkbox','play_sounds',$GLOBALS['LANG']->lang_admin->play_sounds);
			$form['generalsettings'][1]->add_field('checkbox','show_smilies',$GLOBALS['LANG']->lang_admin->show_smilies);
			$form['generalsettings'][1]->add_field('checkbox','show_timestamps',$GLOBALS['LANG']->lang_admin->show_timestamps);
			$form['generalsettings'][1]->add_field('checkbox','hide_email',$GLOBALS['LANG']->lang_admin->hide_email);
			
			$form['messagestyles'][1]->add_field('select','color',$GLOBALS['LANG']->lang_admin->color,'',$settings['style_selectors']['color']);
			$form['messagestyles'][1]->add_field('select','size',$GLOBALS['LANG']->lang_admin->size,'',$settings['style_selectors']['size']);
			$form['messagestyles'][1]->add_field('select','font',$GLOBALS['LANG']->lang_admin->color,'',$settings['style_selectors']['font']);
			
			if(isset($_GET['update'])){
				$USER->data_cache['settings']['show_styles'] = (x7in::read('show_styles') != '');
				$USER->data_cache['settings']['play_sounds'] = (x7in::read('play_sounds') != '');
				$USER->data_cache['settings']['show_smilies'] = (x7in::read('show_smilies') != '');
				$USER->data_cache['settings']['show_timestamps'] = (x7in::read('show_timestamps') != '');
				$USER->data_cache['settings']['hide_email'] = (x7in::read('hide_email') != '');
				
				$group = x7in::read('group','i');
				if(!in_array($group,$groups))
					$form['accountsettings'][1]->add_error('group',$GLOBALS['LANG']->lang_admin->invalid_group);
				else
					$USER->data_cache['group'] = $group;
				
				$color = x7in::read('color');
				if($color != '' && !in_array($color,$settings['style_selectors']['color']))
					$form['messagestyles'][1]->add_error('color',$GLOBALS['LANG']->lang_admin->invalid_style_choice);
	
				$size = x7in::read('size');
				if($size != '' && !in_array($size,$settings['style_selectors']['size']))
					$form['messagestyles'][1]->add_error('size',$GLOBALS['LANG']->lang_admin->invalid_style_choice);
	
				$font = x7in::read('font');
				if($font != '' && !in_array($font,$settings['style_selectors']['font']))
					$form['messagestyles'][1]->add_error('font',$GLOBALS['LANG']->lang_admin->invalid_style_choice);
	
				$commit_ok = true;
				foreach($form as $form_data){
					if($form_data[1]->has_errors())
						$commit_ok = false;
				}
					
				if($commit_ok){
					$USER->data_cache['settings']['styles']['font'] = $font;
					$USER->data_cache['settings']['styles']['color'] = $color;
					$USER->data_cache['settings']['styles']['size'] = $size;
	
					$USER->commit_changes();
	
					$GLOBALS['OUTPUT']->message = $GLOBALS['LANG']->lang_admin->usersettings_updated;
				}
			}

			$form['accountsettings'][1]->add_default('group',$USER->data_cache['group']);
			
			$form['generalsettings'][1]->add_default('show_styles',$USER->data_cache['settings']['show_styles']);
			$form['generalsettings'][1]->add_default('play_sounds',$USER->data_cache['settings']['play_sounds']);
			$form['generalsettings'][1]->add_default('show_smilies',$USER->data_cache['settings']['show_smilies']);
			$form['generalsettings'][1]->add_default('show_timestamps',$USER->data_cache['settings']['show_timestamps']);
			$form['generalsettings'][1]->add_default('hide_email',$USER->data_cache['settings']['hide_email']);
			
			$form['messagestyles'][1]->add_default('color',$USER->data_cache['settings']['styles']['color']);
			$form['messagestyles'][1]->add_default('size',$USER->data_cache['settings']['styles']['size']);
			$form['messagestyles'][1]->add_default('font',$USER->data_cache['settings']['styles']['font']);
			
			$GLOBALS['OUTPUT']->form = array();
			foreach($form as $form_data){
				$GLOBALS['OUTPUT']->form[] = array($form_data[0],$form_data[1]->get_fields());
			}
			
			echo tpl_edituser::usersettings();
			return;
		break;
		case 'userprofile':
			
			$form = new form();
			
			$form->add_field('text','email',$GLOBALS['LANG']->lang_admin->email);
			$form->add_field('checkbox','hide_email',$GLOBALS['LANG']->lang_admin->hide_email);
			$form->add_field('text','status',$GLOBALS['LANG']->lang_admin->status);
			$form->add_field('text','name',$GLOBALS['LANG']->lang_admin->name);
			$form->add_field('text','location',$GLOBALS['LANG']->lang_admin->location);
			$form->add_field('text','hobbies',$GLOBALS['LANG']->lang_admin->hobbies);
			$form->add_field('text','avatar',$GLOBALS['LANG']->lang_admin->avatar);
			$form->add_field('select','gender',$GLOBALS['LANG']->lang_admin->gender,'',array($GLOBALS['LANG']->lang_admin->male=>1,$GLOBALS['LANG']->lang_admin->female=>2));
			$form->add_field('text','password',$GLOBALS['LANG']->lang_admin->password);
			
			if(isset($_GET['update'])){
				
				$USER->data_cache['settings']['hide_email'] = (x7in::read('hide_email') != '');
			
				$email = x7in::read('email');
				
				if($email != $USER->data_cache['email']){
					$act = new lib_user($email,'email');
					
					if(!preg_match('/^[^@]*@[^.]*\..*$/',$email))
						$form->add_error('email',$GLOBALS['LANG']->lang_admin->email_invalid);
					elseif($act->is_valid())
						$form->add_error('email',$GLOBALS['LANG']->lang_admin->email_inuse);
					else
						$USER->data_cache['email'] = $email;
						
				}
				
				$pass = x7in::read('password');
				if(!empty($pass)){
					$USER->data_cache['password'] = sha1($pass);
				}
				
				$USER->data_cache['profile']['name'] = x7in::read('name');
				$USER->data_cache['profile']['location'] = x7in::read('location');
				$USER->data_cache['profile']['hobbies'] = x7in::read('hobbies');
				$USER->data_cache['profile']['gender'] = x7in::read('gender');
				$USER->data_cache['profile']['avatar'] = x7in::read('avatar');
				$USER->data_cache['status'] = x7in::read('status');
				
				if(!$form->has_errors()){
					$USER->commit_changes();
					$GLOBALS['OUTPUT']->message = $GLOBALS['LANG']->lang_admin->usersettings_updated;
				}
				
			}
			
			$form->add_default('email',$USER->data_cache['email']);
			$form->add_default('hide_email',$USER->data_cache['settings']['hide_email']);
			$form->add_default('status',$USER->data_cache['status']);
			
			foreach($USER->data_cache['profile'] as $key=>$def){
				$form->add_default($key,$def);
			}
			
			$GLOBALS['OUTPUT']->form = array(array($GLOBALS['LANG']->lang_admin->userprofile,$form->get_fields()));
			
			echo tpl_edituser::userprofile();
			return;
		break;
		case 'userpermissions':
			// TODO: Decide whether submit buttons should be centored or left-aligned and imlement
			
			$valid_perms = array(	'sending_perms'=>array('SEND_GLOBAL_MESSAGE','SEND_SYS_MESSAGE','SEND_IM_MESSAGE','READ_UNVOICED_USERS','GLOBAL_VOICE','GLOBAL_ACCESS','BAN_IMMUNITY'),
						'roomaxs_perms'=>array('BECOME_INVISIBLE','SEE_INVISIBLE','ENTER_FULL_ROOM','VIEW_ROOM_LOG'),
						'roomctrl_perms'=>array('BAN_KICK_USER','MUTE_USER','VIEW_IP','CREATE_ROOM','SET_ROOM_LOG','EDIT_ROOM_SETTINGS','EDIT_ROOM_PERMS','EDIT_ROOM_ACCESS'),
						'other_perms'=>array('OVERRIDE_DISABLE','VIEW_HIDDEN_EMAILS'),
						'user_perms'=>array('LOG_PM','SEND_OFFLINE_MESSAGE','USE_BUDDY_LIST'),
						'admin_perms'=>array('ACP_MANAGEMENT','ACP_EDITUSER_USERPERMISSIONS','ACP_UPDATEMANAGER','ACP_MANAGEBANS','ACP_MANAGEGROUPS','ACP_MANAGEROOMS','ACP_SETTINGS','ACP_INTEGRATION','ACP_APPEARANCE','ACP_MANAGELANG'),
						);
			
			$GLOBALS['OUTPUT']->forms = array();
			
			foreach($valid_perms as $perm_section=>$pinfo){
				
				$form = new form();
				
				foreach($pinfo as $permname){
					$form->add_field('select',$permname,$GLOBALS['LANG']->lang_admin->$permname,'',array($GLOBALS['LANG']->lang_admin->allow=>1,$GLOBALS['LANG']->lang_admin->inherit=>0,$GLOBALS['LANG']->lang_admin->deny=>-1));
					
					if(isset($_GET['update'])){
						$USER->set_permission($permname,(int)$_POST[$permname]);
					}
					
					$form->add_default($permname,$USER->local_permission_value($permname));
				}
				
				$GLOBALS['OUTPUT']->forms[$GLOBALS['LANG']->lang_admin->$perm_section] = $form->get_fields();
			}
			
			if(isset($_GET['update'])){
				$USER->commit_changes();
				$GLOBALS['OUTPUT']->message = $GLOBALS['LANG']->lang_admin->usersettings_updated;
			}
			
			echo tpl_edituser::userpermissions();
			return;
		break;
	}
	
?>